﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace UI.Admin.Content.ModelOfficeFile
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler,IRequiresSessionState
    {


        public void ProcessRequest(HttpContext context)
        {
            // 验证
            // string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "ContentAdmin", context.Request["ColumnID"]);
            //if (auth != "ACCEPT")
            //  {
            //      return;
            //  }

            context.Response.ContentType = "text/plain";

            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = ((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加办公文件模型的内容
            if (dowhat == "add_officefile")
            {
                string ColumnID = context.Request["ColumnID"];

                //最后编辑者
                string FileEditor = UserName;

                string FileName = context.Request["FileName"];
                string FileType = context.Request["FileType"];
                if (context.Request["FileType"] != "")
                { FileType = context.Request["FileType"].Replace(".", ""); }
                string FileSize = context.Request["FileSize"];
                if (context.Request["FileSize"] != "")
                { FileSize = Utility.Tool.ByteConversionGBMBKB(context.Request["FileSize"]); }
                string FileUrl=context.Request["FileUrl"];
                string UploadTime = DateTime.Now.ToString();
                string UploadSource = context.Request["UploadSource"];

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Create.InsertOfficeFile(ColumnID, FileName, FileEditor, FileType, FileSize, FileUrl, UploadTime, UploadSource) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加办公文件模型栏目【" + Name + "】" +FileName + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 修改办公文件模型的内容
            if (dowhat == "update_officefile")
            {
                string ID = context.Request["ID"];
                string ColumnID = context.Request["ColumnID"];

                // 修改最后编辑者
                string FileEditor = UserName;


                string FileName = context.Request["FileName"];                            
                string UploadTime = DateTime.Now.ToString();
                string UploadSource = context.Request["UploadSource"];

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;
                if (BLL.Update.UpdateOfficeFile(ID,FileName,FileEditor,UploadTime,UploadSource) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "修改办公文件模型栏目【" + Name + "】" + FileName + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除办公文件模型的内容
            if (dowhat == "delete_officefile")
            {
                string ID = context.Request["ID"];

                string FileName = BLL.Find.FindOfficeFile(ID).FileName;
                string Name = BLL.Find.FindOneColumn(context.Request["ColumnID"]).Name;

                if (BLL.Delete.DeleteOfficeFile(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除办公文件模型栏目【" + Name + "】" + FileName + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}